﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarBlackBox
{
    public partial class SerialCommunication
    {
        public object[] ProcCommand(CommunicationCommand Command, params object[] paramList)
        {
            if (!OpenSerialPort())
                return null;
            byte[] Tx = CommunicationFrame.TxFrame(Command, paramList);
            if (Tx == null)
                return null;
            _COM.Write(Tx, 0, Tx.Length);
#if ComDebug
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < Tx.Length; i++)
            {
                str.Append(Tx[i].ToString("X2"));
                str.Append(" ");
            }
            str.Append("\r\n");
            CommunicationDebugForm.ExternShowTx(str.ToString());
#endif
            ushort needLen = CommunicationFrame.RxFrameLen(Command);
            byte[] RxList = new byte[CommunicationFrame.非数据 + needLen];
            RS485Result ret = Receive(needLen, ref RxList);            
            CloseSerialPort();
            if (ret != RS485Result.OK)
                return null;
            if (RxList[2] != (byte)Command)
            {
                MessageBox.Show("通信失败，记录仪回应命令号有误!!");
                return null;
            }
            return CommunicationFrame.ResolveFrame(Command, RxList);
        }
    }
}
